package org.flowable.app.rest.listener;

import cmbchina.util.AjaxJson;
import org.flowable.app.cusTaskExe.CusScriptTask;
import org.flowable.app.cusTaskExe.CusSpecialScriptTask;
import org.flowable.app.cusTaskExe.CusTaskOptParams;
import org.flowable.app.model.runtime.CompleteFormRepresentation;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.slf4j.LoggerFactory;

/**
 * Created by zm on 17-11-5.
 */
public class CusSpecialScriptTaskAutoExeListener implements TaskListener {


    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CusSpecialScriptTaskAutoExeListener.class);

    @Override
    public void notify(DelegateTask listener) {

        logger.info("--------CusSpecialScriptTaskAutoExeListener listener---------");
        String taskDefinitionKey = listener.getTaskDefinitionKey();
        String processInstanceId = listener.getProcessInstanceId();
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        String taskId = listener.getId();
        TaskService taskService = processEngine.getTaskService();

        AjaxJson ajaxJson = new CusTaskOptParams().optCompleteFormRepresentation(listener,taskDefinitionKey,processInstanceId);

        if(ajaxJson.isSuccess()){
            CompleteFormRepresentation completeTaskFormRepresentation = (CompleteFormRepresentation) ajaxJson.getObj();
            new CusSpecialScriptTask().exe(completeTaskFormRepresentation, taskService, taskId, listener.getDescription(),listener.getProcessInstanceId(),taskDefinitionKey);
        }
    }



}
